package org.cypress.rtpack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cypress.rtpack.common.core.Result;
import org.cypress.rtpack.common.exception.BizException;
import org.cypress.rtpack.common.exception.ExceptionCode;
import org.cypress.rtpack.entity.RtpackQutag;
import org.cypress.rtpack.mapper.RtpackQutagMapper;
import org.cypress.rtpack.service.IRtpackQutagService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author getty
 */
@Service
public class RtpackQutagServiceImpl extends ServiceImpl<RtpackQutagMapper, RtpackQutag> implements IRtpackQutagService {

    @Override
    public Result addQutag(RtpackQutag rtpackQutag) {
        if (Objects.isNull(rtpackQutag)) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        boolean success = save(rtpackQutag);
        return success ? Result.ok() : Result.fail("添加趋势标签失败");
    }

    @Override
    public Result deleteQutag(String id) {
        if (Objects.isNull(id)) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        boolean success = removeById(id);
        return success ? Result.ok() : Result.fail("删除趋势标签失败");
    }
    @Override
    public Result deleteQutagByth(String th) {
        if (Objects.isNull(th)) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        QueryWrapper<RtpackQutag> wrapper = new QueryWrapper<>();
        wrapper.eq("th", th); // 或 tag_name，根据你的实际数据库字段
        boolean success = this.remove(wrapper);
        return success ? Result.ok() : Result.fail("删除趋势标签失败");
    }

    @Override
    public Result updateQutag(RtpackQutag rtpackQutag) {
        if (Objects.isNull(rtpackQutag) || Objects.isNull(rtpackQutag.getId())) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        boolean success = updateById(rtpackQutag);
        return success ? Result.ok() : Result.fail("更新趋势标签失败");
    }

    @Override
    public Result getQutagList() {
        List<RtpackQutag> list = list();
        return Result.ok(list);
    }

    @Override
    public Result getQutagById(String id) {
        if (Objects.isNull(id)) {
            throw new BizException(ExceptionCode.valueOf("1"));
        }
        RtpackQutag rtpackQutag = getById(id);
        return Result.ok(rtpackQutag);
    }

    @Override
    public List<RtpackQutag> getQutagByth(String th) {
        QueryWrapper<RtpackQutag> wrapper = new QueryWrapper<>();
        wrapper.eq("th",th);  // 只查这两个字段
        List<RtpackQutag> list = this.list(wrapper);
        return list;
    }
}